package com.yunfangdata.service.impl;

import com.yunfangdata.domain.Employee;
import com.yunfangdata.domain.Staff;
import com.yunfangdata.mapper.EmployeeMapper;
import com.yunfangdata.mapper.StaffMapper;
import com.yunfangdata.query.QueryObject;
import com.yunfangdata.service.IStaffService;
import com.yunfangdata.util.PageResult;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StaffServiceImpl implements IStaffService {

    @Autowired
    private StaffMapper mapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    public void save(Staff entity) {
        mapper.insert(entity);

        Employee employee = new Employee();
        employee.setUsername(entity.getJobNumber()+"");//账号
        employee.setDept(entity.getDepartment());//部门
        employee.setRealName(entity.getName());//真实姓名
        employee.setTel(entity.getPhone());//电话
        employee.setEmail(entity.getEmail());//邮件
        employee.setInputTime(entity.getDuration());//入职日期
        employee.setState(Employee.NORM);//状态
        employee.setAdmin(false);//超管
        System.out.println(employee);
       /* String hashAlgorithmName = "MD5";//加密方式
        Object crdentials = "123";//密码原值
        Object salt = employee.getUsername();//盐值
        int hashIterations = 3;//加密3次
        String s = new SimpleHash(, crdentials, salt, hashIterations).toString();*/

        //密码MD5 加盐,加密 -------- 默认密码123
        String admin = new Md5Hash("1", "admin", 3).toString();
        System.out.println(admin);//超管

        String password = new Md5Hash("123", employee.getUsername(), 3).toString();

        employee.setPassword(password);
        employeeMapper.insert(employee);

    }

    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public void update(Staff entity) {
        mapper.updateByPrimaryKey(entity);
    }

    public Staff get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<Staff> list() {
        return mapper.selectAll();
    }

    public PageResult query(QueryObject qo) {
        int total = mapper.queryForCount(qo);

        if (total == 0){
            return new PageResult();
        }
        List<Staff> rows = mapper.queryForList(qo);
        return new PageResult(total,rows);
    }
}
